﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace SimpleTaskSystem
{
    /// <summary>
    /// 一个任务步骤
    /// </summary>
    [Serializable]
    public class Task
    {
        public string taskName;
        /// <summary>
        /// 是否自动执行
        /// </summary>
        public bool isAutomatic = false;
        /// <summary>
        /// 是否按顺序执行
        /// </summary>
        public bool sequence = true;
        public string description;
        public List<Operation> operations = new List<Operation>();
        public delegate void TaskHandler(Task task);
        public event TaskHandler onTaskStart;
        public event TaskHandler onCompleted;
        /// <summary>
        /// 是否完成，一般只用在非顺序执行情况下
        /// </summary>
        Dictionary<Operation, bool> completedOperations = new Dictionary<Operation, bool>();

        int currentIndex = -1;

        /// <summary>
        /// 初始化任务
        /// </summary>
        public void InitTask()
        {
            foreach (var item in operations)
            {
                completedOperations.Add(item, false);
            }
        }
        /// <summary>
        /// 执行
        /// </summary>
        public void Excute()
        {
            foreach (var item in operations)
            {
                if (isAutomatic)
                {
                    item.isAutomatic = isAutomatic;
                }
                if (!sequence)
                {
                    item.onCompleted += OneOperationCompleted;
                }
            }
            SetNextOperation();
        }

        public Operation GetCurrentOperation()
        {
            if (currentIndex < 0 || currentIndex > operations.Count)
            {
                return null;
            }
            return operations[currentIndex];
        }

        void Completed()
        {
            if (onCompleted != null)
            {
                onCompleted(this);
            }
        }

        void OneOperationCompleted(Operation operation)
        {
            if (sequence)
            {
                if (operation == operations[currentIndex])
                {
                    operations[currentIndex].onCompleted -= OneOperationCompleted;
                }
            }
            else
            {
                if (completedOperations.ContainsKey(operation))
                {
                    completedOperations[operation] = true;
                }
            }
            SetNextOperation();
        }

        void SetNextOperation()
        {
            if (sequence)//如果为按顺序执行
            {
                currentIndex++;
                if (currentIndex >= operations.Count)
                {
                    Completed();
                    return;
                }
            }
            else//不按顺序执行
            {
                int next = currentIndex;
                while (next == -1 || completedOperations[operations[next]])
                {
                    next++;
                    if (next >= operations.Count)
                    {
                        Completed();
                        return;
                    }
                }
                if (next != currentIndex)
                {
                    currentIndex = next;
                }
                else
                {
                    return;
                }
            }
            operations[currentIndex].onCompleted += OneOperationCompleted;
            operations[currentIndex].Ready();
        }
    }
}
